#!/bin/bash

##
## sname(name)
##
sname() {
    echo $1@`hostname`
}

##
## start(name, daemon)
##
start() {
    SNAME=$(sname $1)
    DAEMON=""
    if [ $2 -eq 1 ]
    then
	DAEMON='-sasl sasl_error_logger {file,"/tmp/sasl-'$SNAME'.log"} -detached -noshell'
    fi
    $ERL \
        -boot start_sasl \
        -sname $SNAME \
	-pa $ROOT/ebin \
	-emc 'emc.root_dir'              '"\"'$ROOT'\""' \
             'emc.port.binary'           '"\"EMCPort\""' \
             'emc.cpool.min'             '"5"' \
             'emc.cpool.max'             '"10"' \
	     'emc.cpool.hosts'           '"[{\"localhost\", 11211}]"' \
	     'emc.cpool.behavior'        '"[ {no_block, default},
				             {tcp_nodelay, default}, \
					     {hash, md5}, \
					     {distribution, consistent}, \
					     {cache_lookups, default}, \
					     {use_udp, default}, \
					     {support_cas, default}, \
					     {poll_timeout, default}, \
					     {buffer_requests, false}, \
					     {verify_key, false}, \
					     {sort_hosts, false}, \
					     {connect_timeout, default} ]"' \
	     'emc.logger.event_handlers' '"[{emc.util.console_logger, [info, warning, error, debug]}]"' \
        -s launcher start $DAEMON
}

ROOT=$(dirname $0)
ERL=@ERL@

case "$1" in

  start)
    if [ $# -ne "2" ]
    then
       echo "Usage: `basename $0` start <node name>"
       exit 1
    fi
    start $2 0
    ;;

  daemon)
    if [ $# -ne "2" ]
    then
       echo "Usage: `basename $0` daemon <node name>"
       exit 1
    fi
    start $2 1
    ;;

  *)
    echo "Usage: `basename $0` { start <node name> | daemon <node name> }"
    echo ""
    exit 1
esac

exit 0
